package com.nf.health.app.mine.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.nf.health.app.AppModel;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.HealthAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.chatservice.activity.ChatActivity;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.models.DoctorVo;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;
import com.nf.health.app.zxing.CaptureActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 健康助手
 * Created by windoes on 2016/3/16.
 */
public class MyhealthActivity extends BaseActivity {

    @Bind(R.id.myheallth_list)
    DragListView myheallthList;
    private ImageView titlebar_right_view;

    private int page = 1;
    private int page_size = 10;


    String doctorid;

    int position1;

    private List<DoctorVo> mlist = new ArrayList<DoctorVo>();

    private HealthAdapter healthAdapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_myheallth);

        ButterKnife.bind(this);

        ViewUtil.setText(this, R.id.titlebar_title_tv, "健康助手");
        titlebar_right_view = ViewUtil.getView(this, R.id.titlebar_right_view);
        titlebar_right_view.setImageResource(R.drawable.data_scancode);
        titlebar_right_view.setOnClickListener(this);

        myheallthList.setRefreshableAndLoadMoreable(true, true);

        myheallthList.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                page = 1;
                initData("refresh");
            }

            @Override
            public void onLoadMore() {
                page++;
                initData("more");
            }
        });
        healthAdapter = new HealthAdapter(this,mlist);
        myheallthList.setAdapter(healthAdapter);
        healthAdapter.setOnMyClickListener(new OnMyClickListener() {
            @Override
            public void onClick(int position, String id) {

                if("zxan".equals(id)){
                    doctorid = mlist.get(position).getDoctorid();
                    Bundle bundle = new Bundle();
                    bundle.putString(EaseConstant.EXTRA_USER_ID, doctorid);
                    bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
                    bundle.putString(ChatActivity.EXTRA_DOCTOR_TYPE,"40_");
                    ActivityUtils.showActivity(MyhealthActivity.this, ChatActivity.class, bundle);
                }else{
                    position1 = position;
                    provider.requestDeleteHealthHelperList(id, "Delete_Health");//请求助手列表
                }


            }
        });
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.titlebar_right_view:
                ActivityUtils.showActivity(this, CaptureActivity.class);
                break;
        }
    }

    @Override
    protected void onRefresh() {
        super.onRefresh();
        page = 1;
        initData("refresh");
    }

    //返回结果
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if (actionName.equals("refresh")) {
            myheallthList.completeRefresh();
            List<DoctorVo> contents = (List<DoctorVo>) object;
            mlist.clear();
            mlist.addAll(contents);
            saveListAsEaseUser(mlist);
            if(healthAdapter == null){
                healthAdapter = new HealthAdapter(MyhealthActivity.this, mlist);
                myheallthList.setAdapter(healthAdapter);
            }else{
                healthAdapter.notifyDataRefreshed(mlist);
            }
            if(mlist == null || mlist.size() == 0){
                showDataErrorDefault();
            }else{
                hideDataError();
            }
        }else if(actionName.equals("more")){
            myheallthList.completeLoadMore();
            List<DoctorVo> contents = (List<DoctorVo>) object;

            if(contents.size() < 1){
                showToast("没有更多数据");
            }else{
                mlist.addAll(contents);
                healthAdapter.notifyDataSetChanged();

            }
        }else if(actionName.equals("Delete_Health")){
            mlist.remove(position1);
            healthAdapter.notifyDataSetChanged();//更新数据
            showToast("删除成功");
        }
    }
    protected void onResume() {
        super.onResume();
        initData("refresh");
    }
    private void initData(String flag) {
        provider.requestHealthHelperList("", page + "", page_size + "", flag);//请求助手列表
    }

    /**
     * 把健康助手作为EaseUser保存
     *
     * @param list
     */
    private void saveListAsEaseUser(List<DoctorVo> list) {
        EaseUser user;
        for (DoctorVo modele: list) {
            user = new EaseUser(modele.getDoctorid());
            user.setNick(modele.getDoctorname());
            user.setAvatar(modele.getDoctorhimg());
            AppModel.getInstatnce().saveContact(user);
        }
    }
}
